﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using XNAGameEngine;
using XNAGameEngine.GameObject;
using XNAGameEngine.Utility;
using XNAGameEngine.DataStructures;
using XNAGameEngine.Input;
using Microsoft.Xna.Framework.Input;


namespace XNAGame
{
    public class battlemenu : GameObject2D, IInputListener
    {
        #region Private Vars
        private map currentmap;

        private Warrior currentdude;

        private Warrior tempDude;
        private tile gettile;
        private Warrior[] dudeslist;

        private int[] playerlistIDs;
        private int[] ailistIDs;
        private int totalPlayers;
        private int totalAIs;
        private int[] mapTileIDs;

        private tile[] list;
        private tile[] templist;

        private tile current;
        private tile selected;
        private tile tempSelected;
        private skill selectedskill;

        private int movecounter;
        private int poscounter;
        private int aicounter;

        private int aiselector;

        private bool doingmoving;
        private bool donemoving;
        private bool doingskill;
        private bool doneskill;

        private int templistcounter;
        private int listcounter;

        private int actioncursor;
        private bool actioncursordisp;

        private int menucursor; //0 for move, 1 for action, 2 for end turn
        private bool menucursordisp;

        private int tilecursorx;
        private int tilecursory;
        private bool tilecursordisp;

        private int hasmoved; //0 for no, 1 for hightlight range, 2 for make sure, 3 for yes
        private int hasaction; //0 for not yet, 1 for selector, 2 for highlight range, 3 for make sure, 4 for yes
        public int hasendturn;

        Texture2D _action, _end, _move, _c_down, _c_right, _attack, _burst;
        #endregion

        #region get/set

        public map Currmap
        { get { return this.currentmap; } set { this.currentmap = value; } }

        public Warrior Currdude
        { get { return this.currentdude; } set { this.currentdude = value; } }

        #endregion

        #region Constructor
        public battlemenu (int battlemenuID)           //Battlemenu Constructor
            : base(battlemenuID)
        {
            //Load all the textures for the menu
            _action = GameEngine.GameContent.Load<Texture2D>("Content\\Textures\\battlemenumainaction");
            _end = GameEngine.GameContent.Load<Texture2D>("Content\\Textures\\battlemenumainend");
            _move = GameEngine.GameContent.Load<Texture2D>("Content\\Textures\\battlemenumainmove");
            _c_down = GameEngine.GameContent.Load<Texture2D>("Content\\Textures\\cursordown");
            _c_right = GameEngine.GameContent.Load<Texture2D>("Content\\Textures\\cursorright");
            _attack = GameEngine.GameContent.Load<Texture2D>("Content\\Textures\\battlemenuactionattack");
            _burst = GameEngine.GameContent.Load<Texture2D>("Content\\Textures\\battlemenuactionburst");

            GameEngine.GetService<IManagerInput>().ToggleInputListening(true, this);
        }
        #endregion

        #region Highlight Tiles
        private void highlighttiles()
        {
            for (int k = 0; k < listcounter; k++)
            {
                list[k].Highlight = 1;
            }
            if (tilecursordisp)
            {
                currentmap.Grid[tilecursorx, tilecursory].Highlight = 2;
            }
        }
        private void clearhighlight()
        {
            for (int j = 0; j < 10; j++)
            {
                for (int k = 0; k < 10; k++)
                {
                    currentmap.Grid[j, k].Highlight = 0;
                }
            }
        }
        #endregion

        #region Get moves
        public void getmove()
        {
            listcounter = 0;
            Getmove1(current, 4);
        
            highlighttiles();
            
        }

        public void AIgetmove()
        {
            listcounter = 0;
            for (int a = 0; a < 10; a++)
            {
                for (int b = 0; b < 10; b++)
                {
                    currentmap.grid[a, b].worth = 0;
                }
            }
            AIGetmove1(current, 4);
            for (int k = 0; k < listcounter; k++)
            {
                list[k].Highlight = 1;
            }
            selected = current;
            for (int a = 0; a < listcounter; a++)
            {
               
                    if (selected.worth <= list[a].worth)
                    {
                        selected = list[a];
                    }
                
            }

        }

        private void AIGetmove1(tile start, int range)
        {

            tile[] tempy = new tile[4];
            int counter = 0;


            bool inlist = false;
            int place = 0;
            for (int j = 0; j < listcounter; j++)
            {
                if (currentmap.Grid[(start.X), start.Y].X == list[j].X && currentmap.Grid[(start.X), start.Y].Y == list[j].Y)
                {
                    inlist = true;
                    place = j;
                }

            }
            if (!inlist)
            {
                list[listcounter] = start;
                listcounter++;
            }
            else
            {
                if (list[place].Distance < currentmap.Grid[(start.X), start.Y].Distance)
                {
                    list[place] = start;
                }
            }

            if (range >= 1)
            {

                for (int i = -1; i <= 1; i = i + 2)
                {
                 
                    if (start.X + i >= 0 && start.X + i < 10)
                    {
                        if (currentmap.Grid[(start.X + i), start.Y].Occupiable)
                        {
                            if (currentmap.Grid[(start.X + i), start.Y].Occupant == "")
                            {
                                if (currentmap.Grid[(start.X + i), start.Y].Height - start.Height < 2 && currentmap.Grid[(start.X + i), start.Y].Height - start.Height > -2)
                                {

                                    tempy[counter] = currentmap.Grid[(start.X + i), start.Y];
                                    tempy[counter].Distance = range;
                                    counter++;
                                    AIGetrange(currentmap.Grid[(start.X + i), start.Y], selectedskill.Range);
                                    
                                }
                            }
                        }
                    }
                }
                for (int i = -1; i <= 1; i = i + 2)
                {
                    
                    if (start.Y + i >= 0 && start.Y + i < 10)
                    {
                        if (currentmap.Grid[(start.X), start.Y + i].Occupiable)
                        {
                            if (currentmap.Grid[(start.X), start.Y + i].Occupant == "")
                            {
                                if (currentmap.Grid[(start.X), start.Y + i].Height - start.Height < 2 && currentmap.Grid[(start.X), start.Y + i].Height - start.Height > -2)
                                {

                                    tempy[counter] = currentmap.Grid[(start.X), start.Y + i];
                                    tempy[counter].Distance = range;
                                    counter++;

                                    AIGetrange(currentmap.Grid[(start.X), start.Y + i], selectedskill.Range);
                                }
                            }
                        }
                    }
                }

                for (int k = 0; k < counter; k++)
                {
                    AIGetmove1(tempy[k], range - 1);
                }
            }
        }

        private void AIGetrange(tile start, int range)
        {
            templistcounter = 0;
            AIGetrange1(start, range);

            for (int k = 0; k < templistcounter; k++)
            {
                if (templist[k].warOccupant != null)
                {
                    if (templist[k].warOccupant.team != currentdude.team)
                    {
                        start.worth += 10;
                    }
                }
            }
        }
        private void AIGetrange1(tile start, int range)
        {

            tile[] temp = new tile[4];
            int counter = 0;

            templist[templistcounter] = start;
            templistcounter++;


            if (range >= 1)
            {

                for (int i = -1; i <= 1; i = i + 2)
                {

                    if (start.X + i >= 0 && start.X + i < 10)
                    {
                        bool inlist = false;
                        for (int j = 0; j < templistcounter; j++)
                        {
                            if (currentmap.Grid[(start.X + i), start.Y].X == templist[j].X && currentmap.Grid[(start.X + i), start.Y].Y == templist[j].Y)
                                inlist = true;
                        }
                        if (!inlist)
                        {
                            temp[counter] = currentmap.Grid[(start.X + i), start.Y];
                            counter++;
                        }
                    }
                }
                for (int i = -1; i <= 1; i = i + 2)
                {
                    if (start.Y + i >= 0 && start.Y + i < 10)
                    {
                        bool inlist = false;
                        for (int j = 0; j < templistcounter; j++)
                        {
                            if (currentmap.Grid[(start.X), start.Y + i].X == templist[j].X && currentmap.Grid[(start.X), start.Y + i].Y == templist[j].Y)
                                inlist = true;
                        }
                        if (!inlist)
                        {
                            temp[counter] = currentmap.Grid[(start.X), start.Y + i];
                            counter++;

                        }
                    }
                }
                if (counter > 0)
                {
                    for (int k = 0; k < counter; k++)
                    {
                        AIGetrange1(temp[k], range - 1);
                    }
                }

            }
        }


        private void Getmove1(tile start, int range)
        {

            tile[] tempy = new tile[4];
            int counter = 0;


            bool inlist = false;
            int place = 0;
            for (int j = 0; j < listcounter; j++)
            {
                if (currentmap.Grid[(start.X), start.Y].X == list[j].X && currentmap.Grid[(start.X), start.Y].Y == list[j].Y )
                {
                    inlist = true;
                    place = j;
                }

            }
            if (!inlist)
            {
                list[listcounter] = start;
                listcounter++;
            }
            else
            {
                if (list[place].Distance < currentmap.Grid[(start.X), start.Y].Distance)
                {
                    list[place] = start;
                }
            }

            if (range >= 1)
            {

                for (int i = -1; i <= 1; i = i + 2)
                {
                    if (start.X + i >= 0 && start.X + i < 10)
                    {
                        

                            if (currentmap.Grid[(start.X + i),start.Y].Occupiable)
                            {
                                if (currentmap.Grid[(start.X + i),start.Y].Occupant == "")
                                {
                                    if (currentmap.Grid[(start.X + i),start.Y].Height - start.Height < 2 && currentmap.Grid[(start.X + i),start.Y].Height - start.Height > -2)
                                    {
                                        tempy[counter] = currentmap.Grid[(start.X + i),start.Y];
                                        tempy[counter].Distance = range;
                                        counter++;
                                    }
                                }
                            }
                        
                    }
                }
                for (int i = -1; i <= 1; i = i + 2)
                {

                    if (start.Y + i >= 0 && start.Y + i < 10)
                    {

                            if (currentmap.Grid[(start.X),start.Y + i].Occupiable)
                            {
                                if (currentmap.Grid[(start.X),start.Y + i].Occupant == "")
                                {
                                    if (currentmap.Grid[(start.X),start.Y + i].Height - start.Height < 2 && currentmap.Grid[(start.X),start.Y + i].Height - start.Height > -2)
                                    {
                                        tempy[counter] = currentmap.Grid[(start.X),start.Y + i];
                                        tempy[counter].Distance = range;
                                        counter++;
                                    }
                                }
                            }
                        
                    }
                }

                    for (int k = 0; k < counter; k++)
                    {
                        Getmove1(tempy[k], range - 1);
                    }
    

            }
        }


        public void getrange()
        {
            listcounter = 0;
            Getrange1(current, selectedskill.Range);
            for (int k = 0; k < listcounter; k++)
            {
                list[k].Highlight = 1;
            }
            hasaction = 2;
        }
        private void Getrange1(tile start, int range)
        {

            tile[] temp = new tile[4];
            int counter = 0;

            list[listcounter] = start;
            listcounter++;


            if (range >= 1)
            {

                for (int i = -1; i <= 1; i = i + 2)
                {

                    if (start.X + i >= 0 && start.X + i < 10)
                    {
                        bool inlist = false;
                        for (int j = 0; j < listcounter; j++)
                        {
                            if (currentmap.Grid[(start.X + i),start.Y].X == list[j].X && currentmap.Grid[(start.X + i),start.Y].Y == list[j].Y)
                                inlist = true;
                        }
                        if (!inlist)
                        {
                            temp[counter] = currentmap.Grid[(start.X + i),start.Y];
                            counter++;
                        }
                    }
                }
                for (int i = -1; i <= 1; i = i + 2)
                {
                    if (start.Y + i >= 0 && start.Y + i < 10)
                    {
                        bool inlist = false;
                        for (int j = 0; j < listcounter; j++)
                        {
                            if (currentmap.Grid[(start.X),start.Y + i].X == list[j].X && currentmap.Grid[(start.X),start.Y + i].Y == list[j].Y)
                                inlist = true;
                        }
                        if (!inlist)
                        {
                            temp[counter] = currentmap.Grid[(start.X),start.Y + i];
                            counter++;

                        }
                    }
                }
                if (counter > 0)
                {
                    for (int k = 0; k < counter; k++)
                    {
                        Getrange1(temp[k], range - 1);
                    }
                }

            }
        }


        #region <<<Commented pathfind that doesn't work pay no attention to me>>>
        //public void pathfind()
        //{
        //    templistcounter = 0;
        //    listcounter = 0;
        //    //currentmap.Grid[1, 0].Occupiable = false;
        //    for (int i = 0; i < 10; i++)
        //    {
        //        for (int j = 0; j < 10; j++)
        //        {
        //            currentmap.Grid[i, j].Visited = false;
        //        }
        //    }
        //    pathfind1(current, selected, 0);
        //    pathfind2(current, selected);
        //}

        //private void pathfind1(tile start, tile end, int dist)
        //{
        //    tile[] temp = new tile[4];

        //    int counter = 0;
        //    int place = -1;
        //    bool inlist = false;
        //    for (int j = 0; j < templistcounter; j++)
        //        {
        //            if (currentmap.Grid[(end.X),end.Y].X == templist[j].X && currentmap.Grid[(end.X),end.Y].Y == templist[j].Y)
        //            {
        //                inlist = true;
        //                place = j;
        //            }
        //        }
        //    if (inlist)
        //    {

        //        if (currentmap.Grid[end.X, end.Y].Distance > dist)
        //        {
        //            templist[place].Distance = dist;
        //        }


        //    }
        //    else
        //    {

        //        templist[templistcounter] = end;
        //        templist[templistcounter].Distance = dist;
        //        templistcounter++;



        //        //needs work
        //        if (start.X != end.X || start.Y != end.Y)
        //        {
        //            if (dist < 20)
        //            {
        //                for (int i = -1; i <= 1; i = i + 2)
        //                {
        //                    if (end.X + i >= 0 && end.X + i < 10)
        //                    {
        //                        if (currentmap.Grid[(end.X + i), end.Y].Occupiable)
        //                        {
        //                            if (currentmap.Grid[(end.X + i), end.Y].Occupant == "")
        //                            {
        //                                if (currentmap.Grid[(end.X + i), end.Y].Height - end.Height < 2 && currentmap.Grid[(end.X + i), end.Y].Height - end.Height > -2)
        //                                {
        //                                    temp[counter] = currentmap.Grid[(end.X + i), end.Y];
        //                                    counter++;
        //                                }
        //                            }
        //                        }
        //                    }
        //                    if (end.Y + i >= 0 && end.Y + i < 10)
        //                    {
        //                        if (currentmap.Grid[(end.X), (end.Y + i)].Occupiable)
        //                        {
        //                            if (currentmap.Grid[(end.X), (end.Y + i)].Occupant == "")
        //                            {
        //                                if (currentmap.Grid[(end.X), (end.Y + i)].Height - end.Height < 2 && currentmap.Grid[(end.X), (end.Y + i)].Height - end.Height > -2)
        //                                {
        //                                    temp[counter] = currentmap.Grid[end.X, (end.Y + i)];
        //                                    counter++;
        //                                }
        //                            }
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //        if (counter > 0)
        //        {
        //            for (int k = 0; k < counter; k++)
        //            {
        //                pathfind1(start, temp[k], dist + 1);
        //            }
        //        }
        //    }

        
        //}
        //private void pathfind2(tile start, tile end)
        //{
        //    tile[] tempy = new tile[4];
        //    tile next;
        //    int counter = 0;

        //    list[listcounter] = start;
            
        //    //currentmap.Grid[start.X]
        //    //Visited = true;
            
        //    Console.WriteLine("" + start.X + " " + start.Y + "");

        //    Console.WriteLine("");
        //    listcounter++;

        //    if (start.X != end.X || start.Y != end.Y)
        //    {

        //        for (int i = -1; i <= 1; i = i + 2)
        //        {
        //            if (start.X + i >= 0 && start.X + i < 10)
        //            {
        //                for (int j = 0; j < templistcounter; j++)
        //                {
        //                    if (templist[j].X == start.X + i && start.Y == templist[j].Y)
        //                    {
        //                       // if (templist[j].Visited != true)
        //                       // {
        //                            tempy[counter] = templist[j];
        //                            counter++;
        //                        //}
        //                    }
        //                }
        //            }
        //        }
        //        for (int i = -1; i <= 1; i = i + 2)
        //        {

        //            if (start.Y + i >= 0 && start.Y + i < 10)
        //            {
        //                for (int j = 0; j < listcounter; j++)
        //                {
        //                    if (templist[j].X == start.X && start.Y + i == templist[j].Y)
        //                    {
        //                      //  if (templist[j].Visited != true)
        //                       // {
        //                            tempy[counter] = templist[j];
        //                            counter++;
        //                       // }
        //                    }
        //                }
        //            }
        //        }

        //        if (counter > 0)
        //        {
        //            next = tempy[0];
        //            for (int k = 0; k < counter; k++)
        //            {
    
        //                    if (tempy[k].Distance < next.Distance)
        //                        next = tempy[k];
                        
        //            }

        //            pathfind2(next, end);
                    

        //        }


        //    }
        //}
#endregion

        #endregion


        public void beginturn()
        {
            //Doesn't do anything
        }

        public void endturn()
        {
            //Doesn't do anything

            //templistcounter = 0;
            //listcounter = 0;
            //hasmoved = 0;
            //hasaction = 0;
        }


        #region AI Do skill
        public void AIdoskill()
        {
            if (listcounter > 0)
            {

                for (int k = 0; k < listcounter; k++)
                {
                    if (list[k].warOccupant != null)
                    {
                        if (list[k].warOccupant.team != currentdude.team)
                        {
                            selected = list[k];
                        }
                    }
                }

                if (selected.warOccupant != null)
                {
                    if (selected.warOccupant.team != currentdude.team)
                    {
                        if (selected.warOccupant.CurrentHP >= 1)
                        {
                            currentdude.IsMoving = false;
                            currentdude.IsAttacking = true;
                            Console.WriteLine("Name: " + selected.warOccupant.Name);
                            Console.WriteLine("HP before attck: " + selected.warOccupant.CurrentHP);
                            selected.warOccupant.CurrentHP -= currentdude.AttackPower;
                            Console.WriteLine("HP after attack: " + selected.warOccupant.CurrentHP);
                            selected.warOccupant.IsBeingAttacked = true;

                            if (selected.warOccupant.CurrentHP <= 0)
                            {
                                selected.warOccupant.KillCharacter();
                                selected.Occupant = "";
                                selected.Occupiable = false;
                                selected.warOccupant = null;
                            }
                        }
                    }
                }
            }
            //else
            //{
            //    currentdude.IsMoving = false;
            //    currentdude.IsAttacking = false;
            //}

        }
        #endregion

        #region Do skill
        public void doskill()
        {
            if (selected.warOccupant != null)
            {
                if (selected.warOccupant.team != currentdude.team)
                {
                    if (selected.warOccupant.CurrentHP >= 1)
                    {
                        tempSelected.warOccupant.IsMoving = false;
                        tempSelected.warOccupant.IsAttacking = true;
                        Console.WriteLine("Name: " + selected.warOccupant.Name);
                        Console.WriteLine("HP before attck: " + selected.warOccupant.CurrentHP);
                        selected.warOccupant.CurrentHP -= currentdude.AttackPower;
                        Console.WriteLine("HP after attack: " + selected.warOccupant.CurrentHP);
                        selected.warOccupant.IsBeingAttacked = true;

                        if (selected.warOccupant.CurrentHP <= 0)
                        {
                            selected.warOccupant.KillCharacter();
                            selected.Occupant = "";
                            selected.Occupiable = false;
                            selected.warOccupant = null;
                        }
                    }
                }
            }
            hasaction++;
            clearhighlight();

        }
        #endregion

        #region cursor move functions

        private void movetilecursornorth()
        {
            if (tilecursorx > 0)
            {
                clearhighlight();
                tilecursorx--;
                highlighttiles();

            }
        }
        private void movetilecursorsouth()
        {
            if (tilecursorx < 9)
            {
                clearhighlight();
                tilecursorx++;
                highlighttiles();
            }
        }
        private void movetilecursoreast()
        {
            if (tilecursory < 9)
            {
                clearhighlight();
                tilecursory++;
                highlighttiles();
            }
        }
        private void movetilecursorwest()
        {
            if (tilecursory > 0)
            {
                clearhighlight();
                tilecursory--;
                highlighttiles();
            }
        }

        private void movemenucursorup()
        {
            if (menucursor > 0)
                menucursor--;
            else
                menucursor = 2;
                
        }
        private void movemenucursordown()
        {
            if (menucursor < 2)
                menucursor++;
            else
                menucursor = 0;
        }

        private void moveactioncursorup()
        {

                actioncursor = 0;
        }
        private void moveactioncursordown()
        {

                actioncursor = 1;
        }

        #endregion

        #region Initialize Method
        public override void Initialize()
        {
            menucursor = 0;
            menucursordisp = true;
            actioncursordisp = false;
            tilecursordisp = false;
            actioncursor = 0;
            hasaction = 0;
            hasmoved = 0;

            list = new tile[100];
            templist = new tile[100];
            templistcounter = 0;
            listcounter = 0;
            movecounter = -1;

            donemoving = false;
            doneskill = false;
            doingskill = false;
            doingmoving = false;

            aiselector = 0;

            poscounter = 0;
            aicounter = 0;

            hasendturn = 0;

            base.Initialize();
        }
        #endregion

        #region Update
        public override void Update(GameTime gameTime)
        {
            #region Pose Stuff
            base.Update(gameTime);

            if(currentdude != null)
            {
            if (currentdude.Pose == 0)
            {
          
                if (movecounter < 4 && movecounter != -1)
                {
                    poscounter = 0;
                    if (currentdude.Map_x < selected.X)
                    {
                        currentdude.Facing = "West";
                        currentdude.Pose = 1;
                    }
                    if (currentdude.Map_x > selected.X)
                    {
                        currentdude.Facing = "East";
                        currentdude.Pose = 1;
                    }
                    if (currentdude.Map_y > selected.Y)
                    {
                        currentdude.Facing = "North";
                        currentdude.Pose = 1;
                    }
                    if (currentdude.Map_y < selected.Y)
                    {
                        currentdude.Facing = "South";
                        currentdude.Pose = 1;
                    }
                }
            }


            if (currentdude.Pose == 1)
            {
                if (currentdude.Facing == "North")
                {
                    currentdude._position.X = currentdude._position.X - 2;

                    currentdude._position.Y--;

                    // change sprite here based on poscounter...do the same for the other ones

                    poscounter++;
                }
                if (currentdude.Facing == "East")
                {
                    currentdude._position.X = currentdude._position.X + 2;

                    currentdude._position.Y--;
                    poscounter++;
                }
                if (currentdude.Facing == "South")
                {
                    currentdude._position.X = currentdude._position.X + 2;

                    currentdude._position.Y++;
                    poscounter++;
                }
                if (currentdude.Facing == "West")
                {
                    currentdude._position.X = currentdude._position.X - 2;

                    currentdude._position.Y++;
                    poscounter++;
                }

                if (poscounter >= 16)
                {
                    currentdude.Pose = 0;
                    movecounter++;

                    if (currentdude.Facing == "North")
                    {
                        currentdude.Map_y--;
                        Console.WriteLine("In North");
                    }
                    if (currentdude.Facing == "South")
                    {
                        currentdude.Map_y++;
                        Console.WriteLine("In South");
                    }
                    if (currentdude.Facing == "East")
                    {
                        currentdude.Map_x--;
                        Console.WriteLine("In East");
                    }
                    if (currentdude.Facing == "West")
                    {
                        currentdude.Map_x++;
                        Console.WriteLine("In West");
                    }

                }
            }
            current = currentmap.Grid[currentdude.Map_x, currentdude.Map_y];
            }
            #endregion
            if (currentdude.IsAIcontrolled)
            {

                //    if (aiselector == 0)
                //    {

                //        selectedskill = currentdude.skills[1];
                //        AIgetmove();
                //        aiselector++;
                //    }

                //    if (aiselector == 1)
                //    {
                //        if (current.X == selected.X && current.Y == selected.Y)
                //        {
                //            aiselector++;
                //        }
                //    }


                //        if (aiselector == 2)
                //        {

                //            AIdoskill();

                //            aiselector++;
                //        }
                //        Console.WriteLine("" + selected.X + "," + selected.Y + " | " + current.X + "," + current.Y + ""); 

                //}

                aicounter++;

                if (aicounter >= 129)
                {

                        if (hasmoved == 2)              //third
                        {
                            current.Occupant = "";
                            current.warOccupant = null;
                            selected.Occupant = currentdude.Name;
                            selected.warOccupant = currentdude;
                            movecounter = 0;
                            hasmoved++;
                            menucursor = 1;
                            menucursordisp = true;

                            clearhighlight();

                        }
                        else if (hasmoved == 1)         //second
                        {


                                hasmoved++;
                                tilecursordisp = false;
                        }
                        else if (hasmoved == 0 && menucursordisp && menucursor == 0)  //first
                        {
                            selectedskill = currentdude.skills[1];
                            AIgetmove();
                            hasmoved++;
                            menucursordisp = false;

                        }



                        else if (hasaction == 3)                //seventh
                        {




                            menucursor = 2;
                            tilecursordisp = false;
                            menucursordisp = true;
                            if (listcounter > 0)
                            {
                                movecounter = -1;
                                AIdoskill();
                            }

                            clearhighlight();
                            hasaction++;
                        }
                        else if (hasaction == 2)                //sixth
                        {

                            clearhighlight();
                            hasaction++;
                            tilecursordisp = false;
                        }
                        else if (hasaction == 1)                //fifth
                        {
                            //check!

                            getrange();
                            actioncursordisp = false;

                            hasaction++;
                        }
                        else if (hasaction == 0 && menucursordisp && menucursor == 1)   //fourth
                        {

                            menucursordisp = false;
                            actioncursordisp = true;

                            hasaction++;
                        }


                        else if (menucursordisp == true && menucursor == 2)      //last
                        {
                            hasendturn = 1;
                        }


                        aicounter = 0;
                    }
                }

           

        }
        #endregion

        #region Draw
        public override void Draw(GameTime gameTime)
        {
            #region Draw Menu
            GameEngine.SpriteBatch.Draw(_move, new Vector2((800 - _move.Width), 0), Color.White);
 
            GameEngine.SpriteBatch.Draw(_action, new Vector2((800 - _action.Width), (_move.Height + 1)), Color.White);

            GameEngine.SpriteBatch.Draw(_end, new Vector2((800 - _end.Width), (_move.Height + 34)), Color.White);
            #endregion

            if (menucursordisp == true)
            {
                if (menucursor == 0)
                    GameEngine.SpriteBatch.Draw(_c_right, new Vector2((790 - _move.Width - _c_right.Width), 0), Color.White);
                if (menucursor == 1)
                    GameEngine.SpriteBatch.Draw(_c_right, new Vector2((790 - _action.Width - _c_right.Width), (_move.Height + 1)), Color.White);
                if (menucursor == 2)
                    GameEngine.SpriteBatch.Draw(_c_right, new Vector2((790 - _end.Width - _c_right.Width), (_move.Height + 34)), Color.White);
            }

            if (actioncursordisp == true)
            {
                
                GameEngine.SpriteBatch.Draw(_attack, new Vector2((800 - _move.Width), (_move.Height + 67)), Color.White);
                GameEngine.SpriteBatch.Draw(_burst, new Vector2((800 - _move.Width), (_move.Height + 100)), Color.White);
                if (actioncursor == 0)
                    GameEngine.SpriteBatch.Draw(_c_right, new Vector2((790 - _move.Width - _c_right.Width), (_move.Height + 67)), Color.White);
                if (actioncursor == 1)
                    GameEngine.SpriteBatch.Draw(_c_right, new Vector2((790 - _action.Width - _c_right.Width), (_move.Height + 100)), Color.White);
            }

            if (tilecursordisp == true)
            {
   
                GameEngine.SpriteBatch.Draw(_c_down, new Vector2(currentmap.Grid[tilecursorx, tilecursory].Screenx + 16, (currentmap.Grid[tilecursorx, tilecursory].Screeny - 50)), Color.White);
            }
            

            base.Draw(gameTime);
        }
        #endregion

        #region Destroy
        public override void Destroy()
        {
            base.Destroy();
        }
        #endregion

        #region Load Parties
        public void LoadParties(Warrior[] dudes)
        {
            dudeslist = dudes;

        }
        #endregion

        #region Load Map
        public void LoadMap(map currMap)
        {
            currentmap = currMap;
            mapTileIDs = currentmap.TileIDs;
        }
        #endregion 

        #region IInputListener Members

        public void InputPressed(InputMessage input)
        {
            if (currentdude.IsAIcontrolled != true)
            {
                switch (input._key)
                {
                    case Keys.Left:
                        if (tilecursordisp)
                        {
                            movetilecursorwest();
                        }

                        else
                        {

                        }
                        break;
                    case Keys.Right:
                        if (tilecursordisp)
                        {
                            movetilecursoreast();
                        }

                        else
                        {

                        }

                        break;
                    case Keys.Up:
                        if (tilecursordisp)
                        {
                            movetilecursornorth();
                        }
                        if (actioncursordisp)
                        {
                            moveactioncursorup();
                        }
                        if (menucursordisp)
                        {
                            movemenucursorup();
                        }
                        break;
                    case Keys.Down:
                        if (tilecursordisp)
                        {
                            movetilecursorsouth();
                        }
                        if (actioncursordisp)
                        {
                            moveactioncursordown();
                        }
                        if (menucursordisp)
                        {
                            movemenucursordown();
                        }
                        break;
                    case Keys.Enter:

                        if (hasmoved == 2)
                        {
                            current.Occupant = "";
                            current.warOccupant = null;
                            selected.Occupant = currentdude.Name;
                            selected.warOccupant = currentdude;
                            movecounter = 0;
                            hasmoved++;
                            menucursor = 1;
                            menucursordisp = true;
                            Console.WriteLine("three");
                            clearhighlight();

                        }
                        else if (hasmoved == 1)
                        {
                            bool inlist = false;
                            for (int j = 0; j < listcounter; j++)
                            {
                                if (currentmap.Grid[tilecursorx, tilecursory].X == list[j].X && currentmap.Grid[tilecursorx, tilecursory].Y == list[j].Y)
                                {
                                    if (currentmap.Grid[tilecursorx, tilecursory].X != currentdude.Map_x || currentmap.Grid[tilecursorx, tilecursory].Y != currentdude.Map_y)
                                    {
                                        inlist = true;
                                    }
                                }

                            }
                            if (inlist)
                            {
                                selected = currentmap.grid[tilecursorx, tilecursory];
                                hasmoved++;
                                tilecursordisp = false;
                                Console.WriteLine("two");
                            }
                        }
                        else if (hasmoved == 0 && menucursordisp && menucursor == 0)
                        {
                            getmove();
                            hasmoved++;
                            menucursordisp = false;
                            tilecursorx = current.X;
                            tilecursory = current.Y;
                            tilecursordisp = true;
                            Console.WriteLine("one");
                        }



                        else if (hasaction == 3)
                        {
                            tempSelected = selected;
                            selected = currentmap.grid[tilecursorx, tilecursory];
                            doskill();
                            selected = tempSelected;
                            menucursor = 2;
                            tilecursordisp = false;
                            menucursordisp = true;
                        }
                        else if (hasaction == 2)
                        {
                            selected = currentmap.grid[tilecursorx, tilecursory];
                            clearhighlight();
                            hasaction++;
                            tilecursordisp = false;
                        }
                        else if (hasaction == 1)
                        {
                            //check!
                            selectedskill = currentdude.skills[actioncursor];
                            getrange();
                            actioncursordisp = false;
                            tilecursorx = current.X;
                            tilecursory = current.Y;
                            tilecursordisp = true;
                            hasaction++;
                        }
                        else if (hasaction == 0 && menucursordisp && menucursor == 1)
                        {
                            actioncursor = 0;
                            menucursordisp = false;
                            actioncursordisp = true;

                            hasaction++;
                        }


                        else if (menucursordisp == true && menucursor == 2)
                        {
                            hasendturn = 1;
                        }
                        else if (hasendturn == 1)
                        {
                            endturn();
                            Console.WriteLine("End turn called");
                        }

                        break;
                }
            }
        }

        public void InputReleased(InputMessage input)
        {
            
        }

        public void InputHeld(InputMessage input)
        {
            
        }

        public event ManagerInput.InputRegisterListenerEventHandler ThrowUnregisterListenerEvent;

        #endregion
    }
}
